package com.wxq.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxq.blog.mapper.TagMapper;
import com.wxq.blog.pojo.Tag;
import com.wxq.blog.service.TagService;
import com.wxq.blog.vo.Result;
import com.wxq.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    TagMapper tagMapper;
    @Override
    public Result findAllDetail() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        List<Tag> tags = this.tagMapper.selectList(queryWrapper);
        return Result.success(copyList(tags));
    }

    @Override
    public List<TagVo> findTagsByArticleId(Long articleId) {
        tagMapper.findTagsByArticleId(articleId);
        return null;

    }

    @Override
    public Result findAll() {
        List<Tag> tags = this.tagMapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(tags));
    }

    @Override
    public Result findADetailById(Long id) {
        Tag tag = tagMapper.selectById(id);
        TagVo copy = copy(tag);
        return Result.success(copy);
    }

    public TagVo copy(Tag tag) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag, tagVo);
        return tagVo;
    }
    public List<TagVo> copyList(List<Tag> tagVoList) {
        List<TagVo> taglist = new ArrayList<>();
        for (Tag a : tagVoList) {
            taglist.add(copy(a));
        }
        return taglist;
    }

}
